package Tree;

import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
      public static class BTNode {
          BTNode left;
          BTNode right;
          int value;

          public BTNode(int value) {
              this.value = value;
          }
      }
      private BTNode root;
      int NodeSize = 0;
      int leafCount = 0;
    public BTNode createBinaryTree(){
        BTNode node1 = new BTNode(1);
        BTNode node2 = new BTNode(2);
        BTNode node3 = new BTNode(3);
        BTNode node4 = new BTNode(4);
        BTNode node5 = new BTNode(5);
        BTNode node6 = new BTNode(6);
        root = node1;
        node1.left = node2;
        node2.left = node3;
        node1.right = node4;
        node4.left = node5;
        node4.right = node6;
        return root;
    }

    // 前序遍历
    void preOrder(BTNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.value + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(BTNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.value + " ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrder(BTNode root){
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.value + " ");
    }

    // 获取树中节点的个数    ----1
//    public void size(BTNode root){
//        //计数器
//        if(root == null) {
//            return;
//        }
//        NodeSize++;
//        size(root.left);
//        size(root.right);
//    }
    // 获取树中节点的个数    ----2
    int size(BTNode root){
        //节点数=左+右+1
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }


    // 获取叶子节点的个数------1
    public void getLeafNodeCount1(BTNode root){
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }
    // 获取叶子节点的个数------2
    public int getLeafNodeCount2(BTNode root){
        if(root == null) {
            return 0;
        }
        int count = 0;
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }


    // 子问题思路-求叶子结点个数


// 获取第K层节点的个数
    int getKLevelNodeCount(BTNode root,int k){
        if(root == null) return -1;
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.right,k-1)
                + getKLevelNodeCount(root.left,k-1);
    }


    // 获取二叉树的高度
    int getHeight(BTNode root){
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
//        if(root.right == null && root.left == null) {
//          return 1;
//        }
        return Math.max(rightHeight,leftHeight) + 1;
    }


    // 检测值为value的元素是否存在
    BTNode find(BTNode root, int val){
        if(root == null) return null;
        if(root.value == val) {
            return root;
        }
        BTNode tmp1 = find(root.left,val);
        if(tmp1 != null){
            return tmp1;
        }
        BTNode tmp2 = find(root.right,val);
        if(tmp2 != null){
            return tmp2;
        }
        return null;
    }
    //层序遍历
    void levelOrder(BTNode root){
        if(root == null) return;
        Queue<BTNode> queue = new LinkedList<BTNode>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            BTNode cur = queue.poll();
            System.out.print(cur.value + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(BTNode root){
        return true;
    }
    //相同的树
    public boolean isSameTree(BTNode p, BTNode q) {
        if(p == null && q == null) {
            return true;
        }
        if(q == null || p == null) {
            return false;
        }
        if(p.value != q.value) {
            return false;
        }
        boolean leftNode = isSameTree(p.left,q.left);
        if(!leftNode) {
            return false;
        }
        boolean rightNode = isSameTree(p.right,q.right);
        if(!rightNode) {
            return false;
        }
        return true;
    }
    //化简代码
//    public boolean isSameTree(BTNode p, BTNode q) {
//        if (p == null && q == null) {
//            return true;
//        } else if (p == null || q == null) {
//            return false;
//        } else if (p.value != q.value) {
//            return false;
//        } else {
//            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
//        }
//    }

}
